const app = getApp()
Page({
  data: {
    isShow: false,
    selected: [],
    messageType: '',
    unread: 0,
    announceList: [],
    remindList: [],
    page: 1,
    loading: true
  },
  doEdit(){
    var isShow = !this.data.isShow
    this.setData({
      isShow
    })
  },
  checkboxChange(e){
    var checked = this.data.selected;
    if(e.detail.value.length == 0){
      for(var key in checked){
        if(checked[key] == e.currentTarget.dataset.index){
          checked.splice(key, 1);
        }
      }
    } else {
      checked.push(e.currentTarget.dataset.index)
    }
    this.setData({
      selected: checked
    })
    console.log(this.data.selected)
  },
  doDelete(e){
    var that = this
    wx.showModal({
      title: '确认',
      content: '是否删除所选消息',
      success: function(res) {
        if(res.confirm){
          // 点击了确认
          var selected = that.data.selected
          for(var i = 0; i < selected.length; i++){
            that.data.info.splice(selected[i], 1);
          }
          var isShow = !that.data.isShow
          that.setData({
            isShow,
            info: that.data.info
          })
          console.log('用户点击确定')
        } else {
          console.log('用户点击取消')
        }
      }
    })
  },

  // 获取未读消息个数
  getUnreadCount(url){
    app.sendGetRequest('/community/message/' + url, (res) => {
      console.log(res)
      if(res.code == 200) {
        this.setData({
          unread: res.data
        })
      }
    })
  },
  // 获取公告列表
  getAnnounceList(page) {
    app.sendGetRequest('/community/message/announceList?page=' + page, (res) => {
      console.log(res)
      let announceList = this.data.announceList
      if(res.code == 200) {
        res.data.forEach(item => {
          let announce = {}
          announce.id = item.messageAnnounce.id,
          announce.type = item.messageAnnounce.type,
          announce.content = item.messageAnnounce.content,
          announce.url = item.messageAnnounce.url,
          announce.createTime = item.messageAnnounce.createTime
          announce.status = item.status
          announce.statusText = item.status == 0 ? '未读' : '已读'
          announceList.push(announce)
        })
        this.setData({
          announceList
        })
      }
    })
  },
  // 获取提醒列表
  getRemindList(page) {
    app.sendGetRequest('/community/message/remindList?page=' + page, (res) => {
      console.log(res)
      let remindList = this.data.remindList
      if(res.code == 200) {
        res.data.forEach(item => {
          let remind = {}
          remind.id = item.id,
          remind.senderName = item.senderName,
          remind.senderAction = item.senderAction,
          remind.content = item.object,
          remind.url = item.objectUrl,
          remind.type = item.objectType,
          remind.createTime = item.createTime
          remind.status = item.status
          remind.statusText = item.status == 0 ? '未读' : '已读'
          remindList.push(remind)
        })
        this.setData({
          remindList
        })
      }
    })
  },
  // 跳转到消息详情页
  geToPage(e) {
    let message = e.currentTarget.dataset.message
    let type = e.currentTarget.dataset.type
    console.log(message)
    console.log(type)
    if(message.url == '' || message.url == null) {
      wx.navigateTo({
        url: '/pages/information/infoContent/infoContent?type=' + type
              + '&id=' + message.id + '&status=' + message.status
      })
    } else {
      let url = ''
      let data = {}
      if (type == 'announce') {
        url = 'getAnnounce'
        data = {
          announceId: message.id,
          status: message.status
        }
      } else if (type == 'remind') {
        url = 'getRemind'
        data = {
          id: message.id,
          status: message.status
        }
      }
      app.sendPostRequest('/community/message/' + url, data, (res) => {
        console.log(res)
      }, (err) => {
        console.log(err.errMsg)
      })
      wx.navigateTo({
        url: message.url
      })
    }
  },

  onLoad: function (options) {
    console.log(options.type)
    this.setData({
      messageType: options.type
    })
  },

   /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log('onShow页面返回')
    this.setData({
      page: 1
    })
    if(this.data.messageType == 'announce') {
      this.setData({
        announceList: []
      })
      this.getUnreadCount('getUnreadAnnounceCount')
      this.getAnnounceList(this.data.page)
    } else if (this.data.messageType == 'remind') {
      this.setData({
        remindList: []
      })
      this.getUnreadCount('getUnreadRemindCount')
      this.getRemindList(this.data.page)
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      page: this.data.page + 1
    })
    if (this.data.messageType == 'announce') {
      this.getAnnounceList(this.data.page)
    } else if (this.data.messageType == 'remind') {
      this.getRemindList(this.data.page)
    }
  },

  onUnload: function () {
    var pages = getCurrentPages(); //当前页面
    var prevPage = pages[pages.length - 2]; //前一页
    prevPage.getUnreadMessageCount()
  }
})